// packageI/searchPage/searchPage.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    discoverInfo: [],
    isSotreKey:1, //开启消费券插件
    discoverPage: 1,
    discoverLastPage: 0,
    discoverTap: false,
    // 热门搜索
    hotInfo: [],
    storeInfo: [],
    hotPage: 1,
    hotLastPage: 0,
    hotTap: false,
    keyWord: '',
    searchHistory: []

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if((options.searchPage??'') !== '') {
      this.setData({
        isSotreKey: Number(options.searchPage)
      })
    }
    wx.setNavigationBarTitle({
      title: '搜索',
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(this.data.isSotreKey === 3) {
      this.storeBaseData()
    } else {
      this.getDiscover();
      this.gethot();
    }
    try {
      let History = wx.getStorageSync('searchHistory') || [];
      this.setData({
        searchHistory: JSON.parse(History)
      });
      if(this.data.searchHistory[0]){
        this.setData({
          keyWord : this.data.searchHistory[0].name
        });
      }
    } catch (error) {
      console.log(error);
    }
  },
  goSearch() {
    if(this.data.keyWord.length > 0) {
      if(this.data.isSotreKey === 3) { //开启消费券插件跳转消费券搜索页
        wx.navigateTo({
          url: '/packageF/consumption/search/search?keyword=' + this.data.keyWord
        });
      } else {
        wx.navigateTo({
          url: '/packageB/member/category/search_v2/search_v2?keyword=' + this.data.keyWord,
        });
      }
    } else {
      wx.navigateBack({
        delta: 1,
        fail: (res) => {
          return;
        }
      });
    }
  },
  bindinp(e) {
    let key = e.detail.value;
    this.setData({
      keyWord: key
    });
  },
  tapKeyWord(e) {
    let key = e.currentTarget.dataset.name;
    this.setData({
      keyWord: key
    });
    if(this.data.isSotreKey === 3) {
      wx.navigateTo({  
        url: '/packageF/consumption/search/search?keyword=' + this.data.keyWord,
      });
    } else {
      wx.navigateTo({
        url: '/packageB/member/category/search_v2/search_v2?keyword=' + this.data.keyWord,
      });
    }
  },
  deleHistory() {
    wx.setStorageSync('searchHistory', '');
    this.setData({
      searchHistory: []
    });
  },
  colorFin(a){
    a=a.substring(1);  
    a=a.toLowerCase();  
    let b=new Array();  
    for(let x=0;x<3;x++){  
      b[0]=a.substr(x*2,2);  
      b[3]="0123456789abcdef";  
      b[1]=b[0].substr(0,1);  
      b[2]=b[0].substr(1,1);  
      b[20+x]=b[3].indexOf(b[1])*16+b[3].indexOf(b[2]);  
    }  
    return b[20]+","+b[21]+","+b[22];
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  tapUrl(e) {
    let item = e.currentTarget.dataset.item;
    try {
      wx.navigateTo({
        url: item.mini_url,
        fail: ((res) => {
          wx.switchTab({
            url: item.mini_url
          });
        })
      });
    } catch (error) {
      wx.switchTab({
        url: item.mini_url
      });
    }
  },
  gethot() {
    wx.showLoading({
      title: '加载中',
    });
    let urlStr = app.getNetAddresss('plugin.web-design.frontend.hot.index');
    app._postNetWork({
      url: urlStr,
      data: {
        page: this.data.hotPage
      },
      success: (resdata) => {
        wx.hideLoading();
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            hotInfo: res.data.data,
            hotLastPage: res.data.last_page,
            hotTap: true
          });
        } else {
          this.setData({
            hotInfo:false,
            hotTap:false
          });
        }
      },
      fail: function (res) {
        wx.hideLoading();
      }
    });
  },
  storeBaseData() {  //消费券的搜索关键词
    let urlStr = app.getNetAddresss("plugin.coupon-store.frontend.search.searchBaseData");
    app._postNetWork({
      url: urlStr,
      success:  (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          let inif =  res.data.keyword;
          for(let i =0;i<inif.length;i++){
            inif[i].color = this.colorFin(inif[i].color);
          }
          this.setData({
            discoverInfo: inif,
            storeInfo: res.data.hot,
            discoverLastPage: res.data.last_page,
            discoverTap: true
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getDiscover() {
    wx.showLoading({
      title: '加载中',
    });
    let urlStr = app.getNetAddresss('plugin.web-design.frontend.discover.index');
    app._postNetWork({
      url: urlStr,
      data: {
        page: this.data.discoverPage
      },
      success: (resdata) => {
        wx.hideLoading();
        let res = resdata.data;
        if (res.result == 1) {
          let inif =  res.data.data;
          for(let i =0;i<inif.length;i++){
            inif[i].color = this.colorFin(inif[i].color);
          }
          this.setData({
            discoverInfo: inif,
            discoverLastPage: res.data.last_page,
            discoverTap: true
          });
        } else {
          this.setData({
            discoverInfo:false,
            discoverTap:false
          });
        }
      },
      fail: function (res) {
        wx.hideLoading();
      }
    });
  },
  discoverMore() {
    if (!this.data.discoverTap) {
      return;
    }
    this.data.discoverTap = false;
    if (this.data.discoverPage >= this.data.discoverLastPage) {
      return;
    } else {
      this.setData({
        discoverPage: this.data.discoverPage + 1
      });
      wx.showLoading({
        title: '加载中',
      });
      let urlStr = app.getNetAddresss('plugin.web-design.frontend.discover.index');
      app._postNetWork({
        url: urlStr,
        data: {
          page: this.data.discoverPage
        },
        success: (resdata) => {
          wx.hideLoading();
          let res = resdata.data;
          if (res.result == 1) {
            let inif =  res.data.data;
            for(let i =0;i<inif.length;i++){
              inif[i].color = this.colorFin(inif[i].color);
            }
            this.setData({
              discoverInfo: this.data.concat(inif),
              discoverLastPage: res.data.last_page
            });
            this.data.discoverTap = true;
          } else {
            this.setData({
              discoverPage: this.data.discoverPage - 1
            });
            this.data.discoverTap = true;
            app.tips(res.msg);
          }
        },
        fail: function (res) {
          wx.hideLoading();
        }
      });
    }
  },
  hotMore() {
    if (!this.data.hotTap) {
      return;
    }
    this.data.hotTap = false;
    if (this.data.hotPage >= this.data.hotLastPage) {
      return;
    } else {
      this.setData({
        hotPage: this.data.hotPage + 1
      });
      wx.showLoading({
        title: '加载中',
      });
      let urlStr = app.getNetAddresss('plugin.web-design.frontend.hot.index');
      app._postNetWork({
        url: urlStr,
        data: {
          page: this.data.hotPage
        },
        success: (resdata) => {
          wx.hideLoading();
          let res = resdata.data;
          if (res.result == 1) {
            this.setData({
              hotInfo: this.data.hotInfo(res.data.data),
              hotLastPage: res.data.last_page
            });
            this.data.hotTap = true;
          } else {
            this.setData({
              hotPage: this.data.hotPage - 1
            });
            this.data.hotTap = true;
            app.tips(res.msg);
          }
        },
        fail: function (res) {
          wx.hideLoading();
        }
      });
    }
  }
});